package com.yibai.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yibai.web.admin.entity.TSysRoleMenu;
import com.yibai.web.admin.mapper.RoleMenuMapper;
import com.yibai.web.admin.req.RoleMenuRequest;
import com.yibai.web.admin.service.RoleMenuService;
import com.yibai.web.dto.SuperResponse;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fang
 * @since 2020-07-24
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, TSysRoleMenu> implements RoleMenuService {


    @Override
    public SuperResponse findMenuIdByRid(Integer roleId) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<TSysRoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        List<TSysRoleMenu> list = baseMapper.selectList(wrapper);
        response.setItems(list);
        return response;
    }


    @Override
    public SuperResponse addRoleMenu(RoleMenuRequest roleMenuRequest) {

        SuperResponse superResponse = new SuperResponse();
        deleteRoleMenuId(roleMenuRequest.getRoleId());
        TSysRoleMenu tSysRoleMenu = null;
        if(StringUtils.isNotEmpty(roleMenuRequest.getMenuIds())){
            String meunsId[] = roleMenuRequest.getMenuIds().split(",");
            for(int i=0;i<meunsId.length;i++){
                tSysRoleMenu = new TSysRoleMenu();
                tSysRoleMenu.setMenuId(Integer.parseInt(meunsId[i]));
                tSysRoleMenu.setRoleId(roleMenuRequest.getRoleId());
                baseMapper.insert(tSysRoleMenu);
            }
        }

        return superResponse;
    }

    @Override
    public SuperResponse deleteRoleMenuId(Integer roleId) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<TSysRoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        baseMapper.delete(wrapper);
        return response;
    }
}
